package com.gz.purchase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.library.pojo.MoveInStorageItemInfo;
import com.gz.purchase.library.pojo.StorageCheckProductInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 出库历史记录 界面适配器
 * author boolean . created at 2016/1/26 15:07
 */
public class OutStorageHistoryAdapter extends BaseAdapter {
    private List<MoveInStorageItemInfo> mDataList;
    private Context mContext;

    public OutStorageHistoryAdapter(List<MoveInStorageItemInfo> mDataList, Context mContext) {
        this.mDataList = mDataList;
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.in_storage_history_item,null);
            holder = new ViewHolder();
            holder.tv_storage_name = (TextView) view.findViewById(R.id.tv_storage_name);
            holder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            holder.list_products = (LinearLayout) view.findViewById(R.id.list_products);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        final MoveInStorageItemInfo info = mDataList.get(i);

        holder.tv_storage_name.setText("去往"+info.getStorage_from_name());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm");
        holder.tv_create_time.setText(format.format(new Date(info.getCreate_at()*1000))+"");

        List<StorageCheckProductInfo> products = info.getProductInfoList();
        // 初始化商品条目
        if (products != null){
            holder.list_products.removeAllViews();
            View productItem;
            StorageCheckProductInfo product;
            for (int j = 0 ; j < products.size() ; j++){
                productItem = LayoutInflater.from(mContext).inflate(R.layout.in_storage_history_product_item, null);
                product = products.get(j);
                ((TextView)productItem.findViewById(R.id.tv_product_name)).setText(product.getProduct_name());
                ((TextView)productItem.findViewById(R.id.tv_product_num_and_unit)).setText("1"+product.getProduct_unit()+"*"+product.getProduct_num());
                ((TextView)productItem.findViewById(R.id.tv_shop_name)).setText(product.getShop_name());
                holder.list_products.addView(productItem);
            }
        }

        return view;
    }

    class ViewHolder{
        TextView tv_storage_name;
        TextView tv_create_time;
        LinearLayout list_products;
    }
}
